package creditsProject.client.userOfABank;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.List;

import javax.swing.JFrame;

import creditsProject.client.manager.ManagerGUI;
import creditsProject.dto.Credit;
import creditsProject.dto.RequestDto;

public class ClienUser {

	public static void main(String[] args) {

		try {
			Socket socket = new Socket(InetAddress.getLocalHost(), 32167);
			System.out.println("connected");

			JFrame fr = new UserGUI();
			fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			fr.setVisible(true);

			try (ObjectInputStream ois = new ObjectInputStream(
					socket.getInputStream());
					ObjectOutputStream oos = new ObjectOutputStream(
							socket.getOutputStream());) {
				z: while (true) {

					String com = UserGUI.getCommand();

					if (com != null) {
						switch (com) {
						case "LOGIN":
							System.out.println("login");
							oos.writeObject(new String("USERLOGIN"));
							oos.flush();
							oos.writeObject(UserGUI.getLogin());
							oos.flush();

							String response = (String) ois.readObject();

							x: if (response != null) {

								switch (response) {
								case "USERINVALID":
									// System.out.println("invalid login");
									UserGUI.setLabel("Invalid phone or password SERVER");
									UserGUI.setLogin(null);
									oos.flush();
									break x;

								case "USERLOGINOK":
									System.out.println("LOGIN OK");
									UserGUI.setLabel("");
									fr.setVisible(false);
									UserGUI ug = new UserGUI();
									ug.UserMenu();
									ug.setVisible(true);
									break x;

								default:
									break;
								}
							}
							UserGUI.setCommand("null");
							break;
						case "BYE":
							// System.out.println(UserGUI.getCommand());
							oos.writeObject(new String("BYE"));
							oos.flush();
							break z;
						case "GETAVALCREDITS":
							ManagerGUI.setLabel("");
							oos.writeObject(new String("GETAVALCREDITS"));
							oos.flush();
							List<Credit> coll = (List<Credit>) ois
									.readObject();
							UserGUI.setCredits(coll);
							UserGUI ug2 = new UserGUI();
							ug2.ShowCredits();
							ug2.setVisible(true);
							UserGUI.setCommand("null");
							break;
						case "GETHISTORY":
							ManagerGUI.setLabel("");
							oos.writeObject(new String("GETHISTORY"));
							oos.flush();
							oos.writeObject(UserGUI.getLogin().getPhone());
							oos.flush();
							List<RequestDto> req = (List<RequestDto>) ois
									.readObject();
							UserGUI.setRequests(req);
							UserGUI ug3 = new UserGUI();
							ug3.ShowHistory();
							ug3.setVisible(true);
							UserGUI.setCommand("null");
							break;	
						case "CREATEREQUEST":
							oos.writeObject(new String("CREATEREQUEST"));
							oos.flush();
							oos.writeObject(new HashSet<>(UserGUI.getIdCredits()));
							oos.flush();
							oos.writeObject(UserGUI.getLogin().getPhone());
							oos.flush();
							UserGUI.clearIdCredits();
							UserGUI.setCredits(null);
							UserGUI.setCommand("null");
							UserGUI.setLabel("Request sent to manager");
							break;
						default:
							break;

						}

					}

				}

			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			// System.out.println("THE END");
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}

	}
}
